package settingsapp.settings.toggle;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.widget.Toast;
import ece.settingsapp.R;

// TODO case where device doesn't have bluetooth is not handled
public class SettingBluetooth extends ToggleSettingChanger
{
	BluetoothAdapter bluetoothAdapter;
	public SettingBluetooth(Context context, int intRepresentation)
	{
		super(context, intRepresentation);
		this.context = context;
		try
		{
			bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		}
		catch (Exception e)
		{
			System.err.println("ERROR: failed to find bluetooth service");
		}
	}
	
	protected void setValue(boolean value)
	{
		if (bluetoothAdapter == null)
		{
			Toast.makeText(context, "This device doesn't support bluetooth", Toast.LENGTH_LONG).show();
			return;							// this device doesn't have bluetooth... do nothing
		}
		if (value)
			bluetoothAdapter.enable();
		else
			bluetoothAdapter.disable();
	}

	@Override
	public void readValue()
	{
		if (bluetoothAdapter == null)
			return;							// this device doesn't have bluetooth... do nothing
		isOn = bluetoothAdapter.isEnabled();
		// TODO can read more nuanced information about transitory states through this method
//		bluetoothAdapter.getState()
	}

	@Override
	public int getResourceOn()
	{
		return R.drawable.bluetooth_on;
	}

	@Override
	public int getResourceOff()
	{
		return R.drawable.bluetooth_off;
	}
}
